package com.gitee.wsl.compose.use.coroutines


import androidx.compose.runtime.Composable
import com.gitee.wsl.compose.base.getValue
import com.gitee.wsl.compose.base.setValue
import com.gitee.wsl.compose.base.useRef
import com.gitee.wsl.compose.state.S.state
import com.gitee.wsl.compose.use.SuspendAsyncFn
import kotlin.random.Random

/**
 * Description:
 * @author Junerver
 * date: 2024/3/8-11:28
 * Email: junerver@gmail.com
 * Version: v1.0
 */
@Composable
fun useUpdate(): () -> Unit {
    val (state, setState) = state(0.0)
    return fun() {
        setState(Random.nextDouble())
    }
}

/*
  Description: [useUpdateEffect] 用法等同于 [useEffect]，但是会忽略首次执行，只在依赖更新时执行。
  Author: Junerver
  Date: 2024/3/11-11:19
  Email: junerver@gmail.com
  Version: v1.0
*/

@Composable
fun useUpdateEffect(vararg deps: Any?, block: SuspendAsyncFn) {
    var isMounted by useRef(false)
    useEffect(*deps) {
        if (!isMounted) {
            isMounted = true
        } else {
            block()
        }
    }
}
